using AppodealAds.Unity.Api;
using UnityEngine;
using UnityEngine.UI;

public class MainMenuManager : PanelsManager
{
	public static MainMenuManager menu;

	public int SceneStart;

	public Text CharacterName;

	public GameObject Preloader;

	[HideInInspector]
	public UnitZGameType StartType = UnitZGameType.Single;

	private CharacterCreatorCanvas characterCreator;
    private MyAdManager adManager;

    private void Start()
	{
        adManager = new MyAdManager();
        UnitZ.gameManager.IsPlaying = false;
		UnitZ.Hud.ResetAllHud();
		menu = this;
		MouseLock.MouseLocked = false;
		characterCreator = (CharacterCreatorCanvas)Object.FindObjectOfType(typeof(CharacterCreatorCanvas));
		SceneStart = PlayerPrefs.GetInt("StartScene");
	}

	private void Update()
	{
		if ((bool)CharacterName && (bool)UnitZ.gameManager)
		{
			CharacterName.text = UnitZ.gameManager.UserName;
		}
	}

	public void LevelSelected(int index)
	{
		SceneStart = index;
		PlayerPrefs.SetInt("StartScene", SceneStart);
	}

	public void StopFindGame()
	{
		if ((bool)UnitZ.gameNetwork)
		{
			UnitZ.gameNetwork.StopMatchMaker();
		}
	}

	public void ConnectIP()
	{
		EnterGame(UnitZGameType.Connect);
	}

	public void StartSinglePlayer()
	{
		if ((bool)UnitZ.gameManager)
		{
			UnitZ.gameNetwork.StopMatchMaker();
			EnterGame(UnitZGameType.Single);
		}
	}

	public void HostGameOnline()
	{
		if ((bool)UnitZ.gameNetwork)
		{
            adManager.MyShowNative();
            UnitZ.gameNetwork.StartMatchMaker();
			EnterGame(UnitZGameType.HostOnline);
		}
	}

	public void HostGame()
	{
		if ((bool)UnitZ.gameNetwork)
		{
            adManager.MyShowNative();
            UnitZ.gameNetwork.StopMatchMaker();
			EnterGame(UnitZGameType.HostLan);
		}
	}

	public void FindInternetGame()
	{

		UnitZ.gameNetwork.StartMatchMaker();
		OpenPanelByName("FindGame");
	}

	public void EnterGame(UnitZGameType type)
	{
        adManager.MyShowNative();
        StartType = type;
		if ((bool)characterCreator)
		{
			characterCreator.SetCharacter();
		}
		switch (StartType)
		{
		case UnitZGameType.HostOnline:
			UnityEngine.Debug.Log("Menu select > Host Game Online");
			UnitZ.gameNetwork.HostGame(SceneStart, online: true);
			break;
		case UnitZGameType.HostLan:
			UnityEngine.Debug.Log("Menu select > Host Game");
			UnitZ.gameNetwork.HostGame(SceneStart, online: false);
			break;
		case UnitZGameType.Connect:
			UnityEngine.Debug.Log("Menu select > Connect Game");
			UnitZ.gameNetwork.JoinGame();
			break;
		case UnitZGameType.Single:
			UnityEngine.Debug.Log("Menu select > Single Game");
			UnitZ.gameNetwork.HostGame(SceneStart, online: false);
			UnitZ.gameNetwork.StartServerGame();
			break;
		}
	}

	public void ConnectingDeny()
	{
		UnitZ.gameNetwork.Disconnect();
	}

	public void ExitGame()
	{
        if (Appodeal.isLoaded(3))
		{
			Appodeal.show(3);
		}
		UnitZ.gameNetwork.Disconnect();
		Application.Quit();
        AdManager.Instance.ShowNative();
    }

	public void OpenWebsite(string url)
	{
	}

	public void OpenGDPR()
	{
        adManager.MyShowNative();
        UnityEngine.SceneManagement.SceneManager.LoadScene("GDPR");
	}
}
